package com.sdu.shanyiapi.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.sdu.shanyiapi.db.dao.UserInfoDao;
import com.sdu.shanyiapi.db.pojo.UserInfoEntity;
import com.sdu.shanyiapi.service.UserInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @author lihaopeng
 * @version 1.0
 * @project shanyi-api
 * @description 用户信息
 * @since 2024/4/21 18:16
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Resource
    private UserInfoDao userInfoDao;

    @Override
    @Transactional
    public void insertUserInfo(UserInfoEntity entity) {
        userInfoDao.insertUserInfo(entity);
    }

    @Override
    public HashMap searchUserInfo(int userId) {
        HashMap map = userInfoDao.searchUserInfo(userId);
        if (map != null) {
            String likes = MapUtil.getStr(map, "likes");
            JSONArray array = JSONUtil.parseArray(likes);
            map.replace("likes", array);
            String dislikes = MapUtil.getStr(map, "dislikes");
            array = JSONUtil.parseArray(dislikes);
            map.replace("dislikes", array);
            String diseases = MapUtil.getStr(map, "diseases");
            array = JSONUtil.parseArray(diseases);
            map.replace("diseases", array);
        }
        return map;
    }

    @Override
    @Transactional
    public void updateUserInfo(UserInfoEntity entity) {
        userInfoDao.updateUserInfo(entity);
    }
}
